
from typing import Literal
from copy import deepcopy
from dfJinja import  dfTemplate as  Template
import shelve
from config import database_path
from loguru import logger

CRUD_TYPE=Literal['revise','add_rows','add_columns', 'del_columns','filter']

class crud_table:
    def __init__(self, config: dict) -> None:
        self.config=config
        type:CRUD_TYPE=config['type']
        
        self.input_table_name = config["input_table"]
        self.output_table_name = config.get("output_table", self.input_table_name)
        
        if type=='revise':
            self.revice()
        elif type=='add_rows':
            self.add_rows()
        elif type=='add_columns':
            self.add_columns()
        elif type=='del_columns':
            self.del_columns()
        elif type=='filter':
            self.filter()
    def revice(self):
        with shelve.open(database_path,writeback=True) as db:
            input_table =deepcopy(db['tables_dict'][self.input_table_name])
            output_table =deepcopy(db['tables_dict'].get(self.output_table_name,[]))
            columns=self.config['args']['columns']
            for row in input_table:
                copyrow=deepcopy(row)
                for columninfo in columns:
                    column = columninfo["column"]
                    copyrow.setdefault(column,'')
                    # print(columninfo["value"],row)
                    # 测试表达式
                    value=Template(columninfo["value"]).render(row)
                    
                    copyrow[column]=value.strip()
                output_table.append(copyrow)
            db['tables_dict'][self.output_table_name]=output_table
                
    def add_rows(self):
        with shelve.open(database_path,writeback=True) as db:
            input_table =deepcopy(db['tables_dict'][self.input_table_name])
            output_table =deepcopy(db['tables_dict'].get(self.output_table_name,[]))
            columns=self.config['args']['columns']
            for row in input_table:
                copyrow=deepcopy(row)
                for columninfo in columns:
                    column = columninfo["column"]
                    copyrow.setdefault(column,'')
                    value=Template(columninfo["value"]).render(row)
                    copyrow[column]=value.strip()
                output_table.append(copyrow)
            db['tables_dict'][self.output_table_name]=output_table
                
    def add_columns(self):
        with shelve.open(database_path,writeback=True) as db:
            input_table =deepcopy(db['tables_dict'][self.input_table_name])
            output_table =deepcopy(db['tables_dict'].get(self.output_table_name,[]))
            columns=self.config['args']['columns']
            for row in input_table:
                copyrow=deepcopy(row)
                for columninfo in columns:
                    column = columninfo["column"]
                    copyrow.setdefault(column,'')
                    # print(columninfo["value"],row)
                    value=Template(columninfo["value"]).render(row)
                    copyrow[column]=value.strip()
                output_table.append(copyrow)
            db['tables_dict'][self.output_table_name]=output_table
    
    def del_columns(self):
        with shelve.open(database_path,writeback=True) as db:
            input_table =deepcopy(db['tables_dict'][self.input_table_name])
            output_table =deepcopy(db['tables_dict'].get(self.output_table_name,[]))
            columns=self.config['args']['columns']
            for row in input_table:
                copyrow=deepcopy(row)
                for column in columns:
                    copyrow.pop(column)
                output_table.append(copyrow)
            db['tables_dict'][self.output_table_name]=output_table
    def filter(self):
        with shelve.open(database_path,writeback=True) as db:
            input_table =deepcopy(db['tables_dict'][self.input_table_name])
            output_table =deepcopy(db['tables_dict'].get(self.output_table_name,[]))
            filter=self.config['args']['filter']
            for row in input_table:
                copyrow=deepcopy(row)
                value=Template(filter).render(row).strip().lower()
                if  value == 'true':
                    output_table.append(copyrow)
            db['tables_dict'][self.output_table_name]=output_table
                    
                    
                    



                
            

        
        



